package rabbit.workqueues;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author 汪洋
 * @Description
 */
public class WorkQueue_Producer {
    //声明一个队列名称
    public final static String QUEUE_NAME = "workQueue";
    public static void main(String[] args) throws IOException, TimeoutException {
        //1、构建一个连接工厂对象
        ConnectionFactory factory = new ConnectionFactory();
        //2、对该工厂对象进行相关的配置
        factory.setUsername("root");
        factory.setPassword("root");
        factory.setVirtualHost("/pjedu");
        //3、通过该工厂获取到连接对象
        Connection connection = factory.newConnection();
        //4、通过连接对象来获取到频道对象
        Channel channel = connection.createChannel();
        //5、通过频道对象来创建queue队列
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        for (int i = 1; i <= 10;i++) {
            String message = "Hello WorkQueue!---"+i;
            //6、发送消息,注意,交换机的名称如果写空串,代表使用默认提供的交换机
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        }
        //7、关闭资源
        channel.close();
        connection.close();
    }
}
